package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import com.heima.pojo.TbPartner;
import com.heima.pojo.dto.PartnerDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.TbPartnerVo;
import com.heima.service.ITbPartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 合作商表 前端控制器
 * </p>
 *
 * @author nan
 * @since 2024-06-07
 */
@RestController
@RequestMapping("/api/user-service/partner")
@Api(tags = "合作商管理")
public class TbPartnerController {

    @Autowired
    private ITbPartnerService partnerService;

    @GetMapping("/search")
    @ApiOperation("合作商搜索")
    public PageResponse<TbPartnerVo> getByPage(@RequestParam Integer pageIndex, @RequestParam Integer pageSize, @RequestParam(required = false) String name) {

        return partnerService.getByPage(pageIndex, pageSize, name);
    }

    @PostMapping
    @ApiOperation("新增合作商")
    public Boolean addPartner(@RequestBody PartnerDto partnerDto) {
        return partnerService.save(BeanUtil.toBean(partnerDto, TbPartner.class));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取合作商详情")
    public TbPartner getById(@PathVariable Long id) {
        return partnerService.getById(id);
    }

    @PutMapping("/{id}")
    @ApiOperation("修改合作商")
    public Boolean updatePartner(@PathVariable Integer id, @RequestBody PartnerDto partnerDto) {

        return partnerService.updatePartner(id, partnerDto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除合作商")
    public Boolean deletePartnerById(@PathVariable Integer id) {
        return partnerService.removeById(id);
    }

    @GetMapping("/count")
    @ApiOperation("合作商数量")
    public Integer partnerCount() {
        return partnerService.count();
    }
}
